#!/bin/bash
SCRIPT_BASE_DIR=$( dirname "${BASH_SOURCE[0]}" )
source "${SCRIPT_BASE_DIR}/common/fs_utils.sh"

cmd=$( basename "$0" )
if [ $# -ne 1 ]
then
	echo "Usage: ${cmd} <root_dir>"
	exit 1
fi

exts=(cpp cc c hpp h arxml)
src_dirs=(ara-api sample-applications)
root="$1"
csFile="${root}/cscope.files"

# 检查目录的存在性
for d in "${src_dirs[@]}"
do
  dir_exists_or_die "${d}"
done

echo "Build ${csFile} ..."

# 组织要find的文件扩展名参数(-name ... -o -name ... ...)
find_opts=""
for ext in "${exts[@]}"
do
  find_opts+="-name \"*.${ext}\" -o "
done
# 去掉最后的-o
find_opts=${find_opts%-*}

# 清除旧数据
echo > "${csFile}"
for d in "${src_dirs[@]}"
do
  echo "To find source files in ${d}..."
  # 这里直接调用find会出错，原因未知:(
  bash -c "find ${d} ${find_opts}" >> "${csFile}"
done

{
    echo "Build cscope DB in ${root} ..."
    cd ${root}
    cscope -Rbqk
}

echo "Done."

